<?php

namespace ip138;

use ShidianUtil\Net\IHttpClient;

/**
 * 18030545939 123abc  https://user.ip138.com
 * Class Ip138
 * @package ip138
 */
class Ip138
{
    private IHttpClient $httpClient;

    public function __construct(IHttpClient $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    /**
     * 手机号查询地址
     * @param $phone
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getCityByPhone($phone)
    {
        $token = 'dc486c52be9cbc842ebdfab2dea29d50';

        $uri = 'https://api.ip138.com/mobile/';

        $get = [
            'mobile' => $phone,
            'datatype' => 'jsonp',
        ];

        $headers = [
            'token' => $token,
        ];

        return $this->httpClient
            ->get($uri, $get, $headers);
    }

    /**
     * IP查询地址
     * @param $ip
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getCityByIp($ip)
    {
        $token = '741544a3ef96039c53296a4f47ba88b5';

        $uri = 'http://api.ip138.com/ip/';

        $get = [
            'ip' => $ip,
            'datatype' => 'jsonp',
        ];

        $headers = [
            'token' => $token,
        ];

        return $this->httpClient
            ->get($uri, $get, $headers);
    }
}
